package com.slwish.xty.ui.activity;

import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.slwish.common.base.PresenterToolbarActivity;
import com.slwish.common.config.Contants;
import com.slwish.common.utils.SpUtils;
import com.slwish.xty.R;
import com.slwish.xty.adapter.UserCommentListAdapter;
import com.slwish.xty.entity.UserCommentListModel;
import com.slwish.xty.mvp.contract.MyCommentListContract;
import com.slwish.xty.mvp.presenter.MyCommentListPresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的评价列表
 */
public class MyCommentListActivity extends PresenterToolbarActivity<MyCommentListContract.Presenter> implements MyCommentListContract.View, XRecyclerView.LoadingListener {
    @BindView(R.id.xrecyclerview)
    XRecyclerView xrecyclerview;
    UserCommentListAdapter mAdapter;
    List<UserCommentListModel> mLists = new ArrayList<>();//列表数据集
    private long userId;
//    private LoadService loadService;

    @Override
    protected int getLayoutId() {
        return R.layout.act_my_comment_list;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        userId = SpUtils.getLong(mContext, Contants.UserCode, 0);
        setTitle(R.string.menu_pj_text);
        xrecyclerview.setLayoutManager();
        mAdapter = new UserCommentListAdapter(mLists);
//        mAdapter.bindToRecyclerView(xrecyclerview);
        xrecyclerview.setLoadingListener(this);
        xrecyclerview.setAdapter(mAdapter);
        xrecyclerview.refresh();
    }

    @Override
    protected void initData() {
        super.initData();
        EventBus.getDefault().post("closeDrawer");
//        loadService = LoadSir.getDefault().register(xrecyclerview, new Callback.OnReloadListener() {
//            @Override
//            public void onReload(View v) {
//                loadService.showCallback(LoadingCallback.class);
//            }
//        });
    }

    @Override
    public void queryCommentSuccess(List<UserCommentListModel> model) {
        if (model != null && model.size() > 0) {
            if (mPagerCofig.getCurrentPage() == 1) {
                mLists.clear();
                xrecyclerview.refreshComplete();
            }
            mLists.addAll(model);
            xrecyclerview.setNoMore(false);
            if (mPagerCofig.getCurrentPage() != 1)
                mAdapter.notifyItemRangeChanged(mLists.size() - model.size(), mLists.size() - 1);

        } else {
            if (mPagerCofig.getCurrentPage() == 1) {
                View view = View.inflate(mContext, R.layout.layout_empty, null);
                mAdapter.setEmptyView(view);
                xrecyclerview.refreshComplete();
            }
            xrecyclerview.setNoMore(true);
        }

    }

    /**
     * 加载失败
     */
    @Override
    public void queryCommentFailed() {
        if (mLists == null || mLists.size() < 1) {
            View view = View.inflate(mContext, R.layout.layout_timeout, null);
            mAdapter.setEmptyView(view);
            xrecyclerview.refreshComplete();
            xrecyclerview.setNoMore(true);
            view.findViewById(R.id.tv_reset_loading).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    xrecyclerview.refresh();
                }
            });
        }
    }

    @Override
    protected MyCommentListContract.Presenter initPresenter() {
        return new MyCommentListPresenter(this);
    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        mPagerCofig.setCurrentPage(1);
        mPresenter.queryCommentByUserId(userId, mPagerCofig.getCurrentPage());
    }

    /**
     * 上拉加载
     */
    @Override
    public void onLoadMore() {
        if (mAdapter.getItemCount() / 10 == mPagerCofig.getCurrentPage()) {
            mPagerCofig.setCurrentPage(1 + mPagerCofig.getCurrentPage());
            mPresenter.queryCommentByUserId(userId, mPagerCofig.getCurrentPage());
        } else xrecyclerview.setNoMore(true);
    }
}
